package com.qymerge.manage.system.dao.impl;

import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.system.ro.SysMenuRO;
import com.qymerge.manage.system.dao.ISysMenuDao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

/**
 * Desc
 * User cong.li
 * Date 2017/4/5
 * Time 10:37
 */
@Repository
public class SysMenuDaoImpl extends BaseDao implements ISysMenuDao {

    /**
     * 获取所有菜单
     *
     * @return
     */
    @Override
    public List<SysMenuRO> findAll() {
        String sql = "select * from dwd_MCRS_MDM_sys_menu";
        RowMapper<SysMenuRO> rowMapper = new BeanPropertyRowMapper<SysMenuRO>(SysMenuRO.class);
        return getJdbcTemplate().query(sql, rowMapper);
    }

    /**
     * 查询所有子菜单
     *
     * @param menuId
     * @return
     */
    @Override
    public List<SysMenuRO> findAllByParentId(Long menuId) {
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        String sql = "select * from dwd_MCRS_MDM_sys_menu where parent_id in(:param)";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("param", menuId);
        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<SysMenuRO>(SysMenuRO.class));
    }

    /**
     * 获取所有菜单
     *
     * @param menuIds
     * @return
     */
    @Override
    public List<SysMenuRO> findAllByIds(Set<Long> menuIds) {
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        String sql = "select * from dwd_MCRS_MDM_sys_menu where id in(:param)";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("param", menuIds);
        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<SysMenuRO>(SysMenuRO.class));
    }

    /**
     * 根据
     *
     * @return
     */
    @Override
    public SysMenuRO findOneById(Long id) {
        String sql = "select * from dwd_MCRS_MDM_sys_menu where 1=1";
        List queryList = new ArrayList();
        sql += " and id=?";
        queryList.add(id);

        RowMapper<SysMenuRO> rowMapper = new BeanPropertyRowMapper<SysMenuRO>(SysMenuRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql, rowMapper, queryList.toArray());
        } catch (DataAccessException e) {
            return null;
        }
    }

    /**
     * 保存
     *
     * @param ro
     * @return 主键ID
     */
    @Override
    public int install(SysMenuRO ro) {
        String sql = "insert into dwd_MCRS_MDM_sys_menu(parent_id, menu_name, menu_path, icon_cls, menu_sort, menu_desc, " +
                "create_user_id, create_time, status) values(?,?,?,?,?,?,?,?,?)";

        KeyHolder keyHolder = new GeneratedKeyHolder();
        this.getJdbcTemplate().update(
                new PreparedStatementCreator() {

                    @Override
                    public java.sql.PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                        int i = 0;
                        java.sql.PreparedStatement ps = conn.prepareStatement(sql);
                        ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                        ps.setLong(++i, ro.getParent_id());
                        ps.setString(++i, ro.getMenu_name());
                        ps.setString(++i, ro.getMenu_path());
                        ps.setString(++i, ro.getIcon_cls());
                        ps.setLong(++i, ro.getMenu_sort());
                        ps.setString(++i, ro.getMenu_desc());
                        ps.setLong(++i, ro.getCreate_user_id());
                        ps.setLong(++i, ro.getCreate_time());
                        ps.setInt(++i, ro.getStatus());
                        return ps;
                    }
                }, keyHolder);

        return keyHolder.getKey().intValue();
    }

    /**
     * 更新
     *
     * @param ro
     * @return
     */
    @Override
    public int update(SysMenuRO ro) {
        String sql = "update dwd_MCRS_MDM_sys_menu set menu_name=?, menu_path=?, menu_sort=?, icon_cls=?, menu_desc=?, update_user_id=?, update_time=?, status=? where id=?";

        List param = new ArrayList();
        param.add(ro.getMenu_name());
        param.add(ro.getMenu_path());
        param.add(ro.getMenu_sort());
        param.add(ro.getIcon_cls());
        param.add(ro.getMenu_desc());
        param.add(ro.getCreate_user_id());
        param.add(ro.getCreate_time());
        param.add(ro.getStatus());
        param.add(ro.getId());

        return getJdbcTemplate().update(sql, param.toArray());
    }

    /**
     * 删除菜单
     *
     * @param parentId
     * @return
     */
    @Override
    public int remove(Long parentId, Long menuId) {
        //删除
        String sql = "delete from dwd_MCRS_MDM_sys_menu where 1=1";
        List<Object> queryList = new ArrayList<Object>();

        // 解决页面不传状态问题
        if (null != parentId) {
            sql += " and parent_id=?";
            queryList.add(parentId);
        }

        if (null != menuId) {
            sql += " and id=?";
            queryList.add(menuId);
        }

        return getJdbcTemplate().update(sql, queryList.toArray());
    }

}
